<template>
  <div class="app-container">
    <!--    <el-button   class="filter-item marginRB10" type="primary" icon="el-icon-plus" @click="handleCreate()">添加</el-button>-->
    <!--    <el-button class="marginRB10" type="danger" @click="allDel()" plain>批量删除</el-button>-->
    <el-input
      class="marginRB10"
      v-model="listQuery.user_code"
      placeholder="请输入下单人编号"
      style="width: 200px"
      clearable
    ></el-input>
    <el-select filterable
      v-model="listQuery.status"
      class="filter-item marginRB10"
      placeholder="请选择"
      clearable
      style="width: 200px">
      <el-option
        v-for="item in flagList1"
        :key="item.key"
        :label="item.lab"
        :value="item.key"
      ></el-option>
    </el-select>
    <el-select filterable
      v-model="listQuery.lisence_id"
      class="filter-item marginRB10"
      placeholder="请选择资质"
      clearable
      style="width: 200px"
    >
      <el-option
        v-for="item in searchList.lisence_list"
        :key="item.lisence_id"
        :label="item.lisence_name"
        :value="item.lisence_id"
      ></el-option>
    </el-select>

    <el-button
      type="primary"
      @click="searchList"
      icon="el-icon-search"

      class="marginRB10"
      >搜索</el-button
    >

    <el-table
      ref="dataList"
      :data="menuList"
      style="margin-bottom: 20px"
      row-key="id"
      border
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="id" label="id"> </el-table-column>
      <el-table-column prop="user_code" label="用户编号"> </el-table-column>
      <el-table-column prop="nickname" label="用户昵称"> </el-table-column>
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          {{ scope.row.status === 1 ? "待审核" : "" }}
          {{ scope.row.status === 2 ? "审核失败" : "" }}
          {{ scope.row.status === 3 ? "审核成功" : "" }}
        </template>
      </el-table-column>
      <el-table-column prop="avatar" label="头像地址">
        <template slot-scope="scope">
          <el-popover placement="right" max-width="220" trigger="hover">
            <img
              :src="scope.row.avatar"
              alt=""
              style="max-width: 300px; max-height: 300px"
            />
            <img
              slot="reference"
              :src="scope.row.avatar"
              alt=""
              style="width: 25px; height: 25px"
            />
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="lisence_pic" label="资质图片">
        <template slot-scope="scope">
          <el-popover placement="right" max-width="220" trigger="hover">
            <img
              :src="scope.row.lisence_pic"
              alt=""
              style="max-width: 500px; max-height: 500px"
            />
            <img
              slot="reference"
              :src="scope.row.lisence_pic"
              alt=""
              style="width: 25px; height: 25px"
            />
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="voice" label="声音地址">
        <template slot-scope="scope">
          <svg-icon
            icon-class="stop"
            @click="videoPause()"
            v-if="isAmr && isAmrId === scope.row.id"
            style="font-size: 30px"
          />
          <svg-icon
            icon-class="play"
            @click="videoPlay(scope.row.voice, scope.row.id)"
            v-else
            style="font-size: 30px"
          />
        </template>
      </el-table-column>
      <el-table-column prop="memo" label="接单说明"> </el-table-column>
      <el-table-column prop="admin_id" label="管理员编号"> </el-table-column>
      <el-table-column prop="verify_memo" label="审核意见"> </el-table-column>
      <el-table-column prop="add_time" label="提交时间"> </el-table-column>
      <el-table-column prop="update_time" label="审核时间"> </el-table-column>
      <el-table-column prop="duration" label="录音时长"> </el-table-column>
      <el-table-column prop="license_name" label="资质名字"> </el-table-column>
      <el-table-column prop="tag_name" label="标签"> </el-table-column>
      <el-table-column min-width="180" label="操作">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status === 1"
            type="primary"
            size="mini"
            @click="handleUpdate(scope.row)"
            plain
            >审核</el-button
          >
          <el-button
            type="primary"
            size="mini"
            @click="tagDetail(scope.row)"
            plain
            >标签详情</el-button
          >
          <!--          <el-button v-if="scope.row.status === 1" type="danger" size="mini" @click="handleUpdate(scope.row, '2')" plain>拒绝</el-button>-->
          <!--          <cus-del-btn @ok="handleDelete(scope.row)"></cus-del-btn>-->
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <cus-pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.perPage"
      @pagination="getList"
    />
    <el-dialog
      v-dialogDrag
      :title="titleMap[dialogStatus]"
      :visible.sync="dialogFormVisible"
      @close="handleDialogClose"
    >
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="form"
        label-position="right"
        label-width="120px"
        style="width: 400px; margin: 0 auto"
      >
        <el-form-item label="审核类型" prop="type">
          <el-select filterable
            v-model="form.typeData"
            class="filter-item marginRB10"
            placeholder="请选择审核类型"
            style="width: 200px"
          >
            <el-option
              v-for="item in formType"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="原因" prop="verify_memo">
          <el-input v-model="form.verify_memo" placeholder="请输入原因" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button @click="dialogFormVisible = false">
          取 消
        </el-button>
        <el-button type="primary" @click="submitForm()">
          确 认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  deleteData,
  updateData,
  listData,
  searchList,
  detailData,
} from "@/api/qualifications/qualificationsVerify";
let BenzAMRRecorder = require("benz-amr-recorder");
export default {
  data() {
    return {
      listLoading: true,
      total: 0,
      listQuery: {
        page: 1,
        perPage: 10,
        user_code: undefined,
        status: undefined,
        lisence_id: undefined,
      },
      menuList: [],
      searchList: [],
      dialogFormVisible: false,
      dialogStatus: "",
      titleMap: {
        update: "编辑",
        create: "增加",
      },
      flagList1: [
        { key: undefined, lab: "全部状态" },
        { key: "1", lab: "待审核" },
        { key: "2", lab: "审核失败" },
        { key: "3", lab: "审核成功" },
      ],
      formType: [
        { id: "3", name: "审核成功" },
        { id: "2", name: "审核失败" },
      ],
      form: {
        id: undefined,
        verify_memo: undefined,
        typeData: undefined,
      },
      amr: undefined,
      isAmr: false,
      isAmrId: 0,
      rules: {
        verify_memo: [
          { required: true, message: "请输入名字", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getSearchList();
  },
  methods: {
    // 点击查询
      searchSubmit(){
          this.listQuery.page = 1;
          this.getList()
      },
    getSearchList() {
      searchList().then((response) => {
        this.searchList = response.data;
      });
    },
    getList() {
      this.listLoading = true;
      for (let key in this.listQuery) {
        this.listQuery[key] =
          this.listQuery[key] === "" ? undefined : this.listQuery[key];
      }
      listData(this.listQuery).then((response) => {
        this.menuList = response.data.list;
        this.total = response.data.meta.total;
        this.listLoading = false;
      });
    },
    handleCreate(row) {
      this.resetForm();
      this.dialogStatus = "create";
      this.dialogFormVisible = true;
    },
    handleUpdate(row, type) {
      // this.form = []
      this.form.id = row.id;
      this.form.verify_memo = row.verify_memo;
      // this.form.type= type

      this.dialogStatus = "update";
      this.dialogFormVisible = true;
      //
      // this.form = Object.assign({}, this.form)

      // let data = {id:row.id, type:type}
      // updateData(data).then(response => {
      //   if (response.code === 200) {
      //     this.getList()
      //     this.submitOk('成功')
      //   } else {
      //     this.submitFail('失败')
      //   }
      // })
    },
    handleDelete(row) {
      let data = { ids: row.id };
      deleteData(data).then((response) => {
        if (response.code === 200) {
          this.getList();
          this.submitOk("删除成功");
        } else {
          this.submitFail("删除失败");
        }
      });
    },
    allDel() {

      let delNum = [];
      this.$refs.dataList.selection.forEach((k) => {
        delNum.push(k.id);
      });
      if (delNum.length === 0) {
        return;
      }
      let data = { ids: delNum.toString() };
      deleteData(data).then((response) => {
        if (response.code === 200) {
          this.getList();
          this.submitOk("删除成功");
        } else {
          this.submitFail("删除失败");
        }
      });
    },
    submitForm() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid && this.dialogStatus === "create") {
          let formData = new FormData();
          for (let item in this.form) {
            this.form[item] === undefined
              ? ""
              : formData.append(item, this.form[item]);
          }
          saveData(formData).then((response) => {
            if (response.code === 200) {
              this.getList();
              this.$notify({
                    type:'success',
                    message: '操作成功',
                    position: 'bottom-right',
                    showClose: false,
                    duration: 3000
                });;
              this.dialogFormVisible = false;
            } else {
              this.$message({
                message: response.message,
                type: 'error'
            });;
            }
          });
        } else if (valid && this.dialogStatus === "update") {
          // let formData = new FormData();
          // for(let item in this.form){this.form[item] === undefined? '':formData.append(item, this.form[item]);}
          let data = {
            id: this.form.id,
            verify_memo: this.form.verify_memo,
            type: this.form.typeData,
          };
          updateData(data).then((response) => {
            if (response.code === 200) {
              this.getList();
              this.$notify({
                    type:'success',
                    message: '操作成功',
                    position: 'bottom-right',
                    showClose: false,
                    duration: 3000
                });;
              this.dialogFormVisible = false;
            } else {
              this.$message({
                message: response.message,
                type: 'error'
            });;
            }
          });
        }
      });
    },
    resetForm() {
      this.form = {
        id: undefined,
        name: undefined,
      };
    },
    // 监听dialog关闭时的处理事件
    handleDialogClose() {
      if (this.$refs["dataForm"]) {
        this.$refs["dataForm"].clearValidate(); // 清除整个表单的校验
      }
    },
    videoPause() {
      this.isAmr = false;
      this.amr.stop();
    },
    videoPlay(url, id) {
      this.isAmr = true;
      this.isAmrId = id;
      let that = this;
      if (this.amr !== undefined) {
        this.amr.stop();
      }
      this.amr = new BenzAMRRecorder();
      this.amr.initWithUrl(url).then(function () {
        that.amr.onAutoEnded(function () {
          that.isAmr = false;
        });
        that.amr.play();
      });
    },
    tagDetail(row) {
      detailData({ id: row.id }).then((response) => {
        if (response.code === 200) {
          let str = '';
          for (let i = 0; i < response.data.length; i++) {
            str += `${response.data[i].title}: ${response.data[i].content}<br/>`;
          }
          this.$alert(str?str:'无详情', "标签详情", {
            dangerouslyUseHTMLString: true,
          });

        } else {
          this.$message({
                message: response.message,
                type: 'error'
            });;
        }
      });
      //
    },
  },
};
</script>
